package com.itheima.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.GoodsMapper;
import com.itheima.goods.pojo.Goods;
import com.itheima.goods.service.GoodsService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Override
    public Result findAll() {
        List<Goods> goods = baseMapper.selectList(null);
        return new Result(true, StatusCode.OK, null, goods);
    }

    @Override
    public Result add(Goods goods) {
        int i = baseMapper.insert(goods);
        if (i > 0) {
            return new Result(true, StatusCode.OK, "添加成功");
        }
        return new Result(false, StatusCode.ERROR, "添加失败");
    }

    @Override
    public Result findPage(Integer page, Integer size) {
        //创建分页对象
        Page<Goods> goodsPage = new Page<>(page, size);
        LambdaQueryWrapper<Goods> wrapper = Wrappers.lambdaQuery(Goods.class);
        Page<Goods> goodsPg = baseMapper.selectPage(goodsPage, wrapper);
        PageResult pageResult = new PageResult(goodsPg.getTotal(), goodsPg.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    @Override
    public Result findPage(Integer page, Integer size, Goods searchMap) {
        Page<Goods> goodsPage = new Page<>(page, size);
        LambdaQueryWrapper<Goods> wrappers = Wrappers.lambdaQuery(Goods.class);

        if (searchMap.getSn() != null && !searchMap.getSn().trim().equals("")) {
            wrappers.eq(Goods::getSn, searchMap.getSn());
        }
        if (searchMap.getName() != null && !searchMap.getName().trim().equals("")) {
            wrappers.like(Goods::getName, searchMap.getName());
        }
        if (searchMap.getCaption() != null && !searchMap.getCaption().trim().equals("")) {
            wrappers.like(Goods::getCaption, searchMap.getCaption());
        }
        if (searchMap.getPrice() != null && searchMap.getPrice() >= 0) {
            wrappers.ge(Goods::getPrice, searchMap.getPrice()).orderByAsc(Goods::getPrice);
        }
        if (searchMap.getNum() != null && searchMap.getNum() >= 0) {
            wrappers.ge(Goods::getNum, searchMap.getNum()).orderByAsc(Goods::getNum);
        }
        if (searchMap.getImage() != null && !searchMap.getImage().trim().equals("")) {
            wrappers.eq(Goods::getImage, searchMap.getImage());
        }
        if (searchMap.getCategory() != null && !searchMap.getCategory().trim().equals("")) {
            wrappers.like(Goods::getCategory, searchMap.getCategory());
        }
        if (searchMap.getBrand() != null && !searchMap.getBrand().trim().equals("")) {
            wrappers.like(Goods::getBrand, searchMap.getBrand());
        }
        if (searchMap.getSpec() != null && !searchMap.getSpec().trim().equals("")) {
            wrappers.eq(Goods::getSpec, searchMap.getSpec());
        }
        if (searchMap.getSaleNum() != null && searchMap.getSaleNum() >= 0) {
            wrappers.ge(Goods::getSaleNum, searchMap.getSaleNum()).orderByAsc(Goods::getSaleNum);
        }
        if (searchMap.getCommentNum() != null && searchMap.getCommentNum() >= 0) {
            wrappers.ge(Goods::getCommentNum, searchMap.getCommentNum()).orderByAsc(Goods::getCommentNum);
        }
        if (searchMap.getIsMarketable() != null && !searchMap.getIsMarketable().trim().equals("")) {
            wrappers.eq(Goods::getIsMarketable, searchMap.getIsMarketable());
        }
        if (searchMap.getStatus() != null && !searchMap.getStatus().trim().equals("")) {
            wrappers.eq(Goods::getStatus, searchMap.getStatus());
        }
        if (searchMap.getCreatetime() != null && !searchMap.getCreatetime().equals("")) {
            wrappers.ge(Goods::getCreatetime, searchMap.getCreatetime()).orderByAsc(Goods::getCreatetime);
        }
        if (searchMap.getUpdatetime() != null && !searchMap.getUpdatetime().equals("")) {
            wrappers.ge(Goods::getUpdatetime, searchMap.getUpdatetime()).orderByAsc(Goods::getUpdatetime);
        }

        Page<Goods> goodsPg = baseMapper.selectPage(goodsPage, wrappers);
        PageResult pageResult = new PageResult(goodsPg.getTotal(), goodsPg.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    @Override
    public Result findById(Integer id) {
        Goods goods = baseMapper.selectById(id);
        if (goods!=null){
            return new Result(true,StatusCode.OK,"查询成功",goods);
        }
        return new Result(false,StatusCode.ERROR,"查询失败");
    }

    @Override
    public Result update(Integer id, Goods goods) {
        int i = baseMapper.updateById(goods);
        if (i>0){
            return new Result(true,StatusCode.OK,"修改成功");
        }
        return new Result(false,StatusCode.ERROR,"修改失败");
    }

    @Override
    public Result delete(Integer id) {
        int i = baseMapper.deleteById(id);
        if (i>0){
            return new Result(true,StatusCode.OK,"删除成功");
        }
        return new Result(false,StatusCode.ERROR,"删除失败");
    }
}

